﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFM.API;
using CFM.DTO;
using CFM.Common;
using System.Text.RegularExpressions;

namespace CFM
{
    public partial class frmSalesSatistic : Form
    {
        private SalesSatisticAPI _apiSS = null;
        private CategoriesAPI _apiCate = null;
        private ProductAPI _apiProduct = null;
        public frmSalesSatistic()
        {
            InitializeComponent();
            _apiSS = new SalesSatisticAPI();
            _apiCate = new CategoriesAPI();
            _apiProduct = new ProductAPI();
        }
        
        private void frmSalesSatistic_Load(object sender, EventArgs e)
        {
            dpkDateFrom.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            dpkDateTo.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));

            Loadcbb();
            Search();
        }
        void Loadcbb()
        {
            List<Categories> listCate = _apiCate.GetAll();
            listCate.Add(new Categories() { CategoryId = "-1", CategoryName = "Tất cả" });
            cbxCategory.DataSource = listCate;
            cbxCategory.DisplayMember = "CategoryName";
            cbxCategory.ValueMember = "CategoryId";
            cbxCategory.SelectedValue = "-1";

            List<Product> lisProduct = _apiProduct.GetAll();
            lisProduct.Add(new Product() { ProductId = "-1", ProductName = "Tất cả" });
            cbxProduct.DataSource = lisProduct;
            cbxProduct.DisplayMember = "ProductName";
            cbxProduct.ValueMember = "ProductId";
            cbxProduct.SelectedValue = "-1";
        }
        
        private void btnSalesSatistic_Click(object sender, EventArgs e)
        {
            Search(); 
        }
        private void Search()
        {
            DateTime datefrom = dpkDateFrom.Value;
            DateTime dateTo = dpkDateTo.Value;
            string productId = cbxProduct.SelectedValue.ToString();
            string categoriesID = cbxCategory.SelectedValue.ToString();
            grvSalesSatistic.DataSource = _apiSS.SalesSatistic(categoriesID, productId, datefrom, dateTo);   
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
